package patten.xingwei.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * Description：购物车观察者主题
 * Author：zwenzhi
 * Date：2023/11/17 9:17
 */
public class ShipCarSubject implements ObserverSubject{
    private List<AbstractObserver> observerList = new ArrayList<>();
    private List<Product> productList = new ArrayList<>();
    @Override
    public void addObserver(AbstractObserver abstractObserver) {
        this.observerList.add(abstractObserver);
    }

    @Override
    public void removeObserver(AbstractObserver abstractObserver) {
        this.observerList.remove(abstractObserver);
    }

    @Override
    public void notifyObserver() {
        for (AbstractObserver observer : this.observerList) {
            observer.update(productList);
        }
    }
    public void addProduct(Product product){
        this.productList.add(product);
        notifyObserver();
    }
    public void removeProduct(Product product){
        this.productList.remove(product);
        notifyObserver();
    }
}
